package com.mass.core.sys.user.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.mass.core.framework.aop.LogAop;
import com.mass.core.sys.user.model.SysUser;
import com.mass.core.sys.user.services.SysUserServices;
import com.mass.core.utils.AjaxResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserServices sysUserServices;

    @LogAop(menuName="用户管理",operationDesc="查询用户信息")
    @RequestMapping(value ="/findUserList" ,method = RequestMethod.GET)
    public AjaxResponse findUserList(SysUser sysUser, Integer page,Integer limit){
        return sysUserServices.findUserList(sysUser,page,limit);
    }
    /**
     * 
    * @Title: unlock
    * @Description: 锁屏解锁
    * @param password
    * @return int 返回类型   
    * @throws
     */
    @RequestMapping(value = "/unlock",method = RequestMethod.POST)
    public AjaxResponse unlock(String password,HttpSession session){
    	Object object = session.getAttribute("sysUser");
    	SysUser sysUser = null;
    	if(!ObjectUtils.isEmpty(object)){
    		sysUser = (SysUser) object;
    	}
    	return AjaxResponse.success(sysUserServices.unlock(password,sysUser.getId()));
    }
    
    /**
	 * 查询所有权限
	 * @return
	 */
    @RequestMapping(value = "/findAutorityAll",method = RequestMethod.GET)
    public AjaxResponse findAutorityAll(Long userId){
    	return sysUserServices.findAutorityAll(userId);
    }

    /**
     *修改用户权限
     * @param sysUser
     * @return
     */
    @LogAop(menuName="用户管理",operationDesc="修改用户权限")
    @RequestMapping(value = "/updateAutority",method = RequestMethod.POST)
    public AjaxResponse updateAutority(SysUser sysUser){
        return sysUserServices.updateAutority(sysUser);
    }

    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    @LogAop(menuName="用户管理",operationDesc="添加用户")
    @RequestMapping(value="/insert",method = RequestMethod.POST)
    public AjaxResponse insert(SysUser sysUser){
        return sysUserServices.insert(sysUser);
    }
    
    /**
     * 
    * @Title: updateSysUserInfo
    * @Description: 修改用户信息     不可修改用户名、密码
    * @param sysUser
    * @return int 返回类型   
    * @throws
     */
    @RequestMapping(value="/updateSysUserInfo",method = RequestMethod.POST)
    public AjaxResponse updateSysUserInfo(SysUser sysUser,HttpSession session){
    	Object object = session.getAttribute("sysUser");
    	SysUser user = null;
    	if(!ObjectUtils.isEmpty(object)){
    		user = (SysUser) object;
    		sysUser.setId(user.getId());
    	}
    	return AjaxResponse.success(sysUserServices.updateSysUserInfo(sysUser));
    }
    
    /**
     * 修改用户
     */
    @LogAop(menuName="用户管理",operationDesc="修改用户")
    @RequestMapping(value="/updateSysUser",method = RequestMethod.POST)
    public AjaxResponse updateSysUser(SysUser sysUser,HttpSession session){
    	if(!ObjectUtils.isEmpty(sysUser) &&  !ObjectUtils.isEmpty(sysUser.getPassword())){
    		sysUser.setPassword(DigestUtils.md5Hex(sysUser.getPassword()));
    	}
    	return sysUserServices.updateSysUser(sysUser, session);
    }
    
  
  
}
